﻿using System;
using System.Collections.Generic;

namespace Leaf.NetCore.Cache
{
    /// <summary>
    /// 版本：1.0
    /// 描述：Redis缓存实现类
    /// 日期：2018-11-5 17:48:05
    /// 创建人：王志海
    /// Copyright (c) 2015-2018 首溯科技有限公司
    /// </summary>
    public class RedisCache : ICache
    {
        private static RedisHelper redisHelper;
        public RedisCache(string redisconfig)
        {
            redisHelper = new RedisHelper(redisconfig);
        }
        /// <summary>
        /// 读取缓存
        /// </summary>
        /// <param name="cacheKey">键</param>
        /// <returns></returns>
        public T GetCache<T>(string cacheKey) where T : class
        {
            return redisHelper.Get<T>(cacheKey);
        }

        /// <summary>
        /// 写入缓存
        /// </summary>
        /// <param name="cacheKey">键</param>
        /// <param name="cacheValue">对象数据</param>
        public void SetCache<T>(string cacheKey, T cacheValue) where T : class
        {
            //RedisCache.Set(cacheKey, value);
            //modidfy by chengzg 配置成与webcache相同时间
            SetCache(cacheKey, cacheValue, DateTime.Now.AddMinutes(10));
        }
        /// <summary>
        /// 写入缓存
        /// </summary>
        /// <param name="cacheKey">键</param>
        /// <param name="cacheValue">对象数据</param>
        /// <param name="expireTime">到期时间</param>
        public void SetCache<T>(string cacheKey, T cacheValue, DateTime? expireTime) where T : class
        {
            redisHelper.Set(cacheKey, cacheValue, expireTime.Value);
        }
        /// <summary>
        /// 移除指定数据缓存
        /// </summary>
        /// <param name="cacheKey">键</param>
        public void RemoveCache(string cacheKey)
        {
            redisHelper.Remove(cacheKey);
        }
        /// <summary>
        /// 移除全部缓存
        /// </summary>
        public void RemoveCache()
        {
            redisHelper.RemoveAll();
        }

        public List<string> GetKeys()
        {
            return redisHelper.GetKeys();
        }
    }
}
